package com.base.service;

import java.sql.Timestamp;
import java.util.*;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.hibernate.type.StringType;
import org.hibernate.type.Type;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import tool.ClientTool;
import tool.ImageTool;
import tool.JsonTool;
import tool.StringTool;
import tool.TimeTool;

import com.base.bean.*;
import com.base.dao.*;
import com.base.domain.*;
import com.wechat.service.WechatService;

/**
 * 
 * @author Administrator
 * 
 */

public class ImageService {

	// 单例模式 start
	private volatile static ImageService instance = null;
	private ImageDao imageDao;
	private ImageTool imageTool;

	private ImageService() {
		imageDao = (ImageDao) _BaseDao.ac.getBean("ImageDao");
		imageTool=ImageTool.getInstance();
	
	}

	public static ImageService getInstance() {
		if (instance == null) {
			synchronized (ImageService.class) {// 1
				if (instance == null) {// 2
					instance = new ImageService();// 3
				}
			}
		}
		return instance;
	}
	// 单例模式 end
	
	
	// 完善image信息  type:1 打卡  2：行动
	public Image saveImageInfo(Integer creator,String mediaId,String longitude,String latitude,Integer type){
		Calendar now = Calendar.getInstance();
		Integer year=now.get(Calendar.YEAR);
		Integer month=now.get(Calendar.MONTH)+1;
		String subPath=year.toString()+"/"+month.toString();
		
		Timestamp current = TimeTool.getCurrentTime();		
		long currentTimeMillis=current.getTime();
		String fileName=creator.toString()+"_"+new Long(currentTimeMillis).toString()+"_"+StringTool.getRandom(4)+".jpg";		
		
		String content=AddressService.getInstance().getNameByGps(longitude,latitude);
		
		com.base.bean.Image img=new com.base.bean.Image();
		
	
		
		img.setType(type);
		img.setChecked(0);
		img.setCreated(current);
		img.setCreator(creator);
		
		if(type==1)	//打卡的子路径
			img.setPath("dk/"+subPath);
		else if(type==2)	//行动的子路径
			img.setPath("xd/"+subPath);
		
		
		img.setMediaId(mediaId);
		img.setDiscription(content);
		img.setLatitude(latitude);
		img.setLongitude(longitude);
		img.setTimestr(TimeTool.getNowTime()+"  "+TimeTool.getWeekDay());
		img.setFileName(fileName);
		
		try{
		//判定重复
		
		List<com.base.bean.Image> imgList4p=imageDao.findByHql("from Image a where a.creator="+ creator +" and a.mediaId = '"+mediaId+"' order by a.created desc limit 10");
		if(imgList4p.size()!=0){
			img.setState(3);
//			System.out.println("重复");
			
		}
		else{
			img.setState(0);
			imageDao.merge(img);
		}
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		
		return img;
		
		
	}
	
	
	/**
	 * 
	 * @see 包含了下载和打地址水印
	 */
	public synchronized Image upload(Integer creator,String mediaId,String longitude,String latitude){
	try{
		Calendar now = Calendar.getInstance();
		Integer year=now.get(Calendar.YEAR);
		Integer month=now.get(Calendar.MONTH)+1;
		String subPath=year.toString()+"/"+month.toString();
		
		String accessToken=WechatService.getInstance().find().getAccessToken();
		String content=AddressService.getInstance().getNameByGps(longitude,latitude);
		Image image=imageTool.download(creator,mediaId, accessToken,subPath);
		image=imageTool.markAndMove(creator, image, content);
		image.setPath(year.toString()+"/"+month.toString());
		//存入数据库
		imageDao.merge(image);
		//image=imageDao.findByHql("from Image i where i.fileName='"+image.getFileName()+"'").get(0);
		return image;
	}catch(Exception e){
		e.printStackTrace();
		return null;
	}
		
	}

	
	public synchronized Image upload(Integer creator,String mediaId,String memo){
		try{
			Calendar now = Calendar.getInstance();
			Integer year=now.get(Calendar.YEAR);
			Integer month=now.get(Calendar.MONTH)+1;
			String subPath=year.toString()+"/"+month.toString();
			
			String accessToken=WechatService.getInstance().find().getAccessToken();
			
			Image image=imageTool.download(creator,mediaId, accessToken,subPath);
			image=imageTool.markAndMove(creator, image, memo);
			image.setPath(year.toString()+"/"+month.toString());
			//存入数据库
			imageDao.merge(image);
			//image=imageDao.findByHql("from Image i where i.fileName='"+image.getFileName()+"'").get(0);
			return image;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
			
		}
	
	
	public synchronized Image upload2(Integer creator,String mediaId,String memo){
		try{
			Calendar now = Calendar.getInstance();
			Integer year=now.get(Calendar.YEAR);
			Integer month=now.get(Calendar.MONTH)+1;
			String subPath=year.toString()+"/"+month.toString();
			
			String accessToken=WechatService.getInstance().find().getAccessToken();
			
			Image image=imageTool.download(creator,mediaId, accessToken,subPath);
			//image=imageTool.markAndMove2(creator, image, memo);
			image.setPath(year.toString()+"/"+month.toString());
			//存入数据库
			//imageDao.merge(image);
			//image=imageDao.findByHql("from Image i where i.fileName='"+image.getFileName()+"'").get(0);
			return image;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
			
		}
	

	// test TDD5K4NTMCJHmEOKtK_Aq2XkNuWziKT5deZJJ3kcF2xVWNcdGCR3QuI_MYQXJbS_
	//  lo: 104.1847  la:30.7386 
	public static void main(String[] args) throws Exception {
		ImageService ims=ImageService.getInstance();
		ims.saveImageInfo(1, "mediaId", "101.1", "102.2",1);
		
//		Image image=ims.upload(1, "TDD5K4NTMCJHmEOKtK_Aq2XkNuWziKT5deZJJ3kcF2xVWNcdGCR3QuI_MYQXJbS_", "104.1847", "30.7386");
//		
//		if(image==null){
//			System.out.println("未找记录");
//		}else{
//		
//			System.out.println("##################################");
//			System.out.println(image.getFileName());
//			System.out.println(image.getDiscription());
//			System.out.println(image.getState());
//		}
	}

	public ImageDao getImageDao() {
		return imageDao;
	}

	

}
